package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.Admin;
import com.zjxf.base.bean.po.SchoolBanner;
import com.zjxf.base.bean.ro.RoSchoolBanner;
import com.zjxf.base.bean.vo.VoSchoolBanner;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.SchoolBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * controller for Banner
 * code generated by jpa codegen
 *
 * @author ZhangWei
 * Created On 2021/03/23.
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "学校轮播图")
@RequestMapping("/schoolBanner/")
public class SchoolBannerController extends BaseController {

    private final SchoolBannerService schoolBannerService;

    @ApiOperation(value = "回显")
    @GetMapping("/getEntity")
    public JSONResponse getEntity(@NotNull(message = "请填写轮播图id") @RequestParam Long id) throws OperationException {
        SchoolBanner schoolBanner = schoolBannerService.getEntity(id);
        RoSchoolBanner.entity roSchoolBanner = schoolBannerService.getEntityRo(schoolBanner);
        return JSONResponse.success(roSchoolBanner);
    }

    @ApiOperation(value = "分页")
    @GetMapping("/page")
    public JSONResponse page(@Valid @RequestBody VoSchoolBanner.filter filter) {
        Page<SchoolBanner> schoolBannerPage = schoolBannerService.page(filter);
        JSONObject jsonData = schoolBannerService.conversion(schoolBannerPage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "审核")
    @PostMapping("/check")
    public JSONResponse check(@Valid @RequestBody VoSchoolBanner.check check) throws OperationException {
        Admin admin = (Admin) getUser();
        schoolBannerService.check(check, admin);
        return JSONResponse.success(SysConst.SUCCESS);
    }

}